import { reactive } from 'vue';
import { BasicForm } from '@/components/ProForm/interface/index';
import { LOGIN_URL } from '@/config';
import { UserInterface } from '@/api/interface/auth/user';

const userStore = useUserStore();

let changePwdForm = reactive<BasicForm<UserInterface.ReqUserChangePwd>>({
  form: {
    account: '',
    oldPassword: '',
    password: ''
  },
  title: '',
  visible: false,
  formCols: 1,
  labelWidth: [70, 90],
  formAttrs: {
    labelWidth: ''
  },
  options: [
    {
      type: 'input',
      label: 'username',
      hide: false,
      prop: 'account',
      defaultValue: userStore.userInfo.account,
      rules: [{ required: true, message: 'username', trigger: 'blur' }],
      attrs: {
        disabled: true
      }
    },
    {
      type: 'input',
      label: 'oldPassword',
      hide: false,
      defaultValue: '',
      prop: 'oldPassword',
      rules: [{ required: true, message: 'oldPassword', trigger: 'blur' }]
    },
    {
      type: 'input',
      label: 'password',
      hide: false,
      defaultValue: '',
      prop: 'password',
      rules: [{ required: true, message: '新密码', trigger: 'blur' }]
    }
  ]
});

const savePwdForm = async (form: UserInterface.ReqUserChangePwd) => {
  await userChangePwd(form);
  changePwdForm.visible = false;
  // 1.执行退出登录接口
  await logoutApi();

  // 2.清除 Token
  userStore.setToken('');

  // 3.重定向到登陆页
  routerReplace(LOGIN_URL);
  elMessage(transitionLanguages('basic', 'changePasswordSuccess'));
};
export { changePwdForm, savePwdForm };
